package util.sql;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.json.JSONArray;

public class JsonUtil {
	final private String QUOT_MARK = "●★■⊙■☆●";
	final private String QUOT_JSON_ESCAPE = "\\\""; 
	public Object getObj(Class<?> c, String json, int type) {
		Object obj = null;
		try {
			HashMap<String, String> map = new HashMap<String, String>();
			String regJson = json.replace(QUOT_MARK, QUOT_JSON_ESCAPE);
			Pattern p = Pattern.compile("\"([^:\"]+)\":\"(.*?)\"[},]");
			Matcher m = p.matcher(regJson);
			while (m.find()) {
				System.out.println(m.group());
					map.put(m.group(1), m.group(2).replace(QUOT_MARK, "\""));
			}
			obj = c.newInstance();
			Field[] fields = c.getDeclaredFields();
			for (Field field : fields) {
				field.setAccessible(true);
				JsonAnnation jm = field.getAnnotation(JsonAnnation.class);
				if (null != jm) {
					String[] keySet = jm.keySet().split(":");
					type = keySet.length - 1 < type ? keySet.length - 1 : type;
					String key = keySet[type];
					Class<?> classType = field.getType();
					 if(String.class==classType){
						 field.set(obj, map.get(key));
					}else if(Integer.class==classType || int.class==classType){
						field.set(obj, Integer.valueOf(map.get(key)));
					}else if(Boolean.class==classType || boolean.class==classType){
						field.set(obj, Boolean.valueOf(map.get(key)));
					}else if(Long.class==classType || long.class==classType){
						field.set(obj, Long.valueOf(map.get(key)));
					}else if(Character.class==classType || char.class==classType){
						field.set(obj, map.get(key).charAt(0));
					}else if(Byte.class==classType || byte.class==classType){
						field.set(obj, Byte.valueOf(map.get(key)));
					}else if(Float.class==classType || float.class==classType){
						field.set(obj, Float.valueOf(map.get(key)));
					}else if(Double.class==classType || double.class==classType){
						field.set(obj, Double.valueOf(map.get(key)));
					}else if(Short.class==classType || short.class==classType){
						field.set(obj, Short.valueOf(map.get(key)));
					}
				} 
			}
		} catch (Exception e) {
			Log.exception(e);
		}
		
		return obj;
	}

	public String getJson(Object obj, int type, boolean withNullValue) {
		StringBuilder sb = new StringBuilder();
		try {
			sb.append('{');
			Field[] fields = obj.getClass().getDeclaredFields();
			for (Field field : fields) {
				field.setAccessible(true);
				JsonAnnation jm = field.getAnnotation(JsonAnnation.class);
				if (null == jm) {
					continue;
				} else {
					Object value = field.get(obj);
					String[] keySet = jm.keySet().split(":");
					type = keySet.length - 1 < type ? keySet.length - 1 : type;
					String key = jm.keySet().split(":")[type];
					if (null == value) {
						if (withNullValue) {
							sb.append('"');
							sb.append(key);
							sb.append("\":\"\",");
						}
					} else {
						sb.append('"');
						sb.append(key);
						sb.append("\":\"");
						sb.append(String.valueOf(value).replace("\"", QUOT_JSON_ESCAPE));
						sb.append("\",");
					}
				}
			}
			sb.setCharAt(sb.length() - 1, '}');
		} catch (Exception e) {
			e.printStackTrace();
		}

		return sb.toString();
	}

	public List<?> getObjList(Class<?> c, JSONArray array, int type){
		List<Object> list = new ArrayList<Object>();
		try {
			int length = array.length();
			for(int i=0; i<length; i++){
				String jsonStr = array.getString(i);
				Object obj = getObj(c, jsonStr, type);
				list.add(obj);
			}
		} catch (Exception e) {
			Log.exception(e);
		}
		return list;
	}

}
